package com.sunxd.vue.domain.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.google.common.collect.Maps;
import com.sunxd.vue.bean.request.LoginReq;
import com.sunxd.vue.bean.request.LoginCheckRequest;
import com.sunxd.vue.domain.bean.domain.User;
import com.sunxd.vue.domain.bean.enums.ErrorEnum;
import com.sunxd.vue.domain.exception.InternalServerException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;
import java.util.Objects;

/**
 * @author sun.xd
 * @description: description
 * @date 2024/4/2 15:43
 */
@Service
@RequiredArgsConstructor
public class AuthService {

    private final UserService userService;

    public String login(LoginReq req){
        User user = userService.findByUserName(req.getName());
        if(Objects.isNull(user)){
            throw new InternalServerException(ErrorEnum.LOGIN_ERR);
        }
        Map<String,String> paramMap = Maps.newHashMap();
        paramMap.put("id",user.getId().toString());
        paramMap.put("name",user.getName());
        String sign = JWT.create()
                .withClaim("user", paramMap)
                .withExpiresAt(Date.from(LocalDateTime.now().plusMinutes(60L).atZone(ZoneId.systemDefault()).toInstant()))
                .sign(Algorithm.HMAC256("!Q@W#E$R"));
        System.out.println(sign);
        return sign;
    }

    public String login2(LoginCheckRequest req){
        User user = userService.findByUserName(req.getUserName());
        if(Objects.isNull(user)){
            throw new InternalServerException(ErrorEnum.LOGIN_ERR);
        }
        Map<String,String> paramMap = Maps.newHashMap();
        paramMap.put("id",user.getId().toString());
        paramMap.put("name",user.getName());
        String sign = JWT.create()
                .withClaim("user", paramMap)
                .withExpiresAt(Date.from(LocalDateTime.now().plusMinutes(60L).atZone(ZoneId.systemDefault()).toInstant()))
                .sign(Algorithm.HMAC256("!Q@W#E$R"));
        System.out.println(sign);
        return sign;
    }

    public String auth(HttpServletRequest servletRequest){
        String token = servletRequest.getHeader("token");
        if(StringUtils.isBlank(token)){
            throw new InternalServerException(ErrorEnum.NOT_LOGIN);
        }
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("!Q@W#E$R")).build();
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        Map<String, Claim> claims = decodedJWT.getClaims();
        if(!claims.containsKey("user")){
            throw new InternalServerException(ErrorEnum.NOT_LOGIN);
        }
        Claim user = claims.get("user");
        Map<String,String> as = user.as(Map.class);
        return as.get("name");

    }


}
